package org.agilelang.generator.javastruts2;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IFiles;
import org.agilelang.object.Project;

public class GFile extends Generator implements IFiles {

    Project project;

    public GFile(String path_out, Project project) {
        this.path = path_out;
        this.project = project;
        this.template2="javastruts2";
    }
    
    @Override
    public void process() {
        directoriesStatic();
        filesStatic();
    }

    public void directoriesStatic() {
                Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/javastruts2/bootstrap/files/directories.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JAXBContext jc;
        StaticDirectories staticDirectories = null;
        try {
            jc = JAXBContext.newInstance(StaticDirectories.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticDirectories = (StaticDirectories) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticDirectory> directories = staticDirectories.directories;
        for (StaticDirectory directory : directories) {
            this.createDirectory(path + directory.path);
            System.out.println(directory.path);
            //fileRelocateFile("index_root.jsp", path + "/web/index.jsp");
        }
    }
    public void filesStatic() {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/javastruts2/bootstrap/files/files.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JAXBContext jc;
        StaticFiles staticFiles = null;
        try {
            jc = JAXBContext.newInstance(StaticFiles.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticFiles = (StaticFiles) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticFile> files = staticFiles.files;
        for (StaticFile file : files) {
            System.out.println(file.origin);
            if(file.type.equals("file")){
                fileRelocateFile("javastruts2",file.origin, path + file.destination);
            }
            if(file.type.equals("imagen")){
                createImageIcon("javastruts2",file.origin, path + file.destination);
            }
            //fileRelocateFile("index_root.jsp", path + "/web/index.jsp");
        }
    }


/*
    public void menu(List<Menu> menus, String name, String description) {
        String str_menu = "";
        String str_submenu_item = "";
        int t = menus.size();
        for (int i = 0; i < t; i++) {
            Menu menu = menus.get(i);
            String str_submenu = "";
            String str_submenu_li = "";
            String str_submenu_first = "";
            String str_submenu_first_class = "";
            if (menu.menuitems.size() > 0) {
                List<MenuItem> menuitems = menu.menuitems;
                int tm = menuitems.size();
                for (int ii = 0; ii < tm; ii++) {
                    MenuItem menuitem = menuitems.get(ii);
                    str_submenu_li = str_submenu_li + TAB + TAB + TAB + "<li><a id=\"tab-members\" href=\"<s:url value=\"" + "/" + menuitem.controller + "/" + menuitem.action + "\"></s:url>\" <s:if test=\"#currenturl.contains('" + menuitem.controller.toLowerCase() + "')\">class=\"" + menuitem.controller.toLowerCase() + " selected\"</s:if><s:else>class=\"" + menuitem.controller.toLowerCase() + "\"</s:else>>" + menuitem.controller + "</a></li>" + BR;
                    if (ii == 0) {
                        str_submenu_first = "/" + menuitem.controller + "/" + menuitem.action;
                        str_submenu_first_class = menuitem.controller.toLowerCase();
                        str_submenu = str_submenu + "#currenturl.contains('" + menuitem.controller.toLowerCase() + "')";
                    } else {
                        str_submenu = str_submenu + " || #currenturl.contains('" + menuitem.controller.toLowerCase() + "')";
                    }
                }
                str_submenu_item = str_submenu_item
                        + "<s:if test=\"" + str_submenu + "\">" + BR
                        + TAB + "<div class=\"tabs\">" + BR
                        + TAB + TAB + "<ul>" + BR
                        + str_submenu_li
                        + TAB + TAB + "</ul>" + BR
                        + TAB + "</div>" + BR
                        + "</s:if>" + BR;
            }
            str_menu = str_menu
                    + TAB + TAB + TAB + "<li>" + BR
                    + TAB + TAB + TAB + TAB + "<a href=\"<s:url value=\"" + str_submenu_first + "\"></s:url>\" <s:if test=\"" + str_submenu + "\">class=\"" + str_submenu_first_class + " selected\"</s:if>>" + menu.name + "</a>" + BR
                    + TAB + TAB + TAB + "</li>" + BR;
            String file = readStreamTpl("me_header.jsp.tpl");
            file = file.replace("//menu//", str_menu);
            createfile("web/view/layout/header.jsp", path, file);

            file = readStreamTpl("me_innav.jsp.tpl");
            file = file.replace("//submenu//", str_submenu_item);
            createfile("web/view/layout/innav.jsp", path, file);



        }

    }
*/
    public void database(String host, String login, String password, String database) {
        //String file = readStreamMenu("header.jsp.tpl");
        //file = file.replace("//menu//", str_menu);
        //   createfile("web/view/layout/header.jsp", path, file);
    }

    
    /*public void otherFile() {
        createDirectories();

        fileRelocateFile("index_root.jsp", path + "/web/index.jsp");

        fileRelocateFile("style.css", path + "/web/css/style.css");

        createImageIcon("add.png", path + "/web/images/add.png");
        createImageIcon("calendar.png", path + "/web/images/calendar.png");
        createImageIcon("delete.png", path + "/web/images/delete.png");
        createImageIcon("edit.png", path + "/web/images/edit.png");
        createImageIcon("read.png", path + "/web/images/read.png");

        fileRelocateFile("index.jsp", path + "/web/view/application/index.jsp");

        fileRelocateFile("footer.jsp", path + "/web/view/layout/footer.jsp");

        String file = readStreamFiles("header.jsp");
        file = file.replace("//app_name//", this.project.application.name);
        createfile("/web/view/layout/header.jsp", path, file);

        //fileRelocateFile("header.jsp", path + "/web/view/layout/header.jsp");
        fileRelocateFile("innav.jsp", path + "/web/view/layout/innav.jsp");
        fileRelocateFile("layout.jsp", path + "/web/view/layout/layout.jsp");
        fileRelocateFile("topnav.jsp", path + "/web/view/layout/topnav.jsp");

        fileRelocateFile("jquery-ui-1.9.1.custom.min.css", path + "/web/css/smoothness/jquery-ui-1.9.1.custom.min.css");
        fileRelocateFile("jquery-1.8.2.js", path + "/web/js/jquery-1.8.2.js");
        fileRelocateFile("jquery-ui-1.9.1.custom.min.js", path + "/web/js/jquery-ui-1.9.1.custom.min.js");
        createImageIcon("ui-bg_flat_0_aaaaaa_40x100.png", path + "/web/css/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png");
        createImageIcon("ui-bg_flat_75_ffffff_40x100.png", path + "/web/css/smoothness/images/ui-bg_flat_75_ffffff_40x100.png");
        createImageIcon("ui-bg_glass_55_fbf9ee_1x400.png", path + "/web/css/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png");
        createImageIcon("ui-bg_glass_65_ffffff_1x400.png", path + "/web/css/smoothness/images/ui-bg_glass_65_ffffff_1x400.png");
        createImageIcon("ui-bg_glass_75_dadada_1x400.png", path + "/web/css/smoothness/images/ui-bg_glass_75_dadada_1x400.png");
        createImageIcon("ui-bg_glass_75_e6e6e6_1x400.png", path + "/web/css/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png");
        createImageIcon("ui-bg_glass_95_fef1ec_1x400.png", path + "/web/css/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png");
        createImageIcon("ui-bg_highlight-soft_75_cccccc_1x100.png", path + "/web/css/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png");
        createImageIcon("ui-icons_222222_256x240.png", path + "/web/css/smoothness/images/ui-icons_222222_256x240.png");
        createImageIcon("ui-icons_2e83ff_256x240.png", path + "/web/css/smoothness/images/ui-icons_2e83ff_256x240.png");
        createImageIcon("ui-icons_454545_256x240.png", path + "/web/css/smoothness/images/ui-icons_454545_256x240.png");
        createImageIcon("ui-icons_888888_256x240.png", path + "/web/css/smoothness/images/ui-icons_888888_256x240.png");
        createImageIcon("ui-icons_cd0a0a_256x240.png", path + "/web/css/smoothness/images/ui-icons_cd0a0a_256x240.png");



        fileRelocateFile("tiles.xml", path + "/web/WEB-INF/tiles.xml");
        fileRelocateFile("web.xml", path + "/web/WEB-INF/web.xml");

        //----------------------------------------------------------------------
        fileRelocateFile("struts.xml", path + "/src/java/struts.xml");
        fileRelocateFile("AppController.java.tpl", path + "/src/java/controller/AppController.java");
        fileRelocateFile("MainController.java.tpl", path + "/src/java/controller/MainController.java");
        fileRelocateFile("Pagination.java.tpl", path + "/src/java/help/Pagination.java");
    }*/

    /*private void createDirectories() {
        
     this.createDirectory(path + "\\web\\css");
        this.createDirectory(path + "\\web\\css\\smoothness");
        this.createDirectory(path + "\\web\\css\\smoothness\\images");
        this.createDirectory(path + "\\web\\images");
        this.createDirectory(path + "\\web\\view");
        this.createDirectory(path + "\\web\\js");
        this.createDirectory(path + "\\web\\view\\application");
        this.createDirectory(path + "\\web\\view\\layout");

        this.createDirectory(path + "\\src\\java\\controller");
        this.createDirectory(path + "\\src\\java\\help");
        this.createDirectory(path + "\\src\\java\\model");
        this.createDirectory(path + "\\src\\java\\model\\dao");
        this.createDirectory(path + "\\src\\java\\model\\entities");
        this.createDirectory(path + "\\src\\java\\model\\mapping");
    }*/
    
}
